﻿using System;
using System.Disposables;
using System.Linq.Functional;
using System.Windows;
using System.Windows.Controls;

namespace SoftSource.Timecard.Views
{
    public partial class EntrySummaryView : UserControl, IDisposable
    {
        #region Fields
        private CompositeDisposable compositeDisposable;
        #endregion Fields

        public EntrySummaryView()
        {
            InitializeComponent();

            this.compositeDisposable = new CompositeDisposable();
        }

        #region Properties

        public int ExpectedHours
        {
            get { return (int)GetValue(ExpectedHoursProperty); }
            set { SetValue(ExpectedHoursProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ExpectedHours.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ExpectedHoursProperty =
            DependencyProperty.Register("ExpectedHours", typeof(int), typeof(EntrySummaryView), new PropertyMetadata(8));


        public DateTime? Date
        {
            get { return (DateTime?)GetValue(DateProperty); }
            set { SetValue(DateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Date.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DateProperty =
            DependencyProperty.Register("Date", typeof(DateTime?), typeof(EntrySummaryView), new PropertyMetadata(null));


        public decimal Hours
        {
            get { return (decimal)GetValue(HoursProperty); }
            set { SetValue(HoursProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Hours.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HoursProperty =
            DependencyProperty.Register("Hours", typeof(decimal), typeof(EntrySummaryView), new PropertyMetadata(0.0M, (sender, e) =>
            {
                var view = sender as EntrySummaryView;
                decimal currentHours;
                if (view != null && e.NewValue is decimal)
                {
                    string state = string.Empty;
                    currentHours = (decimal)e.NewValue;

                    var expectedHoursPerDay = (int)view.GetValue(EntrySummaryView.ExpectedHoursProperty);

                    state = currentHours.Match()
                        .With<string>(hours => hours == expectedHoursPerDay, "GreenState")
                        .With(hours => hours < expectedHoursPerDay, "OrangeState")
                        .With(hours => hours > expectedHoursPerDay, "BlueState")
                        .With(hours => hours == 0.0M, "RedState")
                        .Do();

                    VisualStateManager.GoToState(view, state, true);

                    string toolTip = view.Date.HasValue ? string.Format("{0}{1}{2}", view.Hours, Environment.NewLine, view.Date.Value.ToShortDateString()) : view.Hours.ToString();
                    ToolTipService.SetToolTip(view, toolTip);
                }
            }));

        #endregion Properties

        #region IDisposable Members

        public void Dispose()
        {
            this.compositeDisposable.Dispose();
            GC.SuppressFinalize(this);
        }

        #endregion

        ~EntrySummaryView()
        {
            this.Dispose();
        }
    }
}
